﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Entity;


public partial class BusinessData_IMAGES_ImageList : PageBase
{
    static decimal count = -1, start_record = 0, end_record = 10, pages = 0, pagesize = 18, curpage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
            this.btnQuery_Click(sender, e);
        }
    }


    private void InitData()
    {
        var ot = new DictionaryItemsManager().GetItemList(DictionaryEnum.PhotoType, MyStateManager.DomainID);
        drpPhotoType.DataSource = ot;
        drpPhotoType.DataBind();
        drpPhotoType.Items.Insert(0,new ListItem("全部", ""));

        drpPhotoType1.DataSource = ot;
        drpPhotoType1.DataBind();

        drpPhotoType2.DataSource = ot;
        drpPhotoType2.DataBind();

        drpPhotoType3.DataSource = ot;
        drpPhotoType3.DataBind();

        drpPhotoType4.DataSource = ot;
        drpPhotoType4.DataBind();

        drpPhotoType5.DataSource = ot;
        drpPhotoType5.DataBind();



    }

    private bool UploadImage(FileUpload file, String photoTypeID)
    {
        if (file.HasFile)
        {
            string filetype = "jpg,png,bmp";
            string oldfilename = file.FileName;//上载文件名
            string ext = oldfilename.Substring(oldfilename.LastIndexOf(".") + 1).ToLower();//上载文件扩展名
            if (filetype.Contains(ext))
            {
                string datapath = DateTime.Now.ToString("yyyyMMdd");
                string newfilename = ConfigurationManager.AppSettings["ImagePath"] + datapath;

                if (!System.IO.Directory.Exists(newfilename))
                {
                    System.IO.Directory.CreateDirectory(newfilename);
                }
                string name = BusinessLogic.getNewImageName() + "." + ext;
                file.SaveAs(newfilename + "/" + name);
                String fileName= datapath + "/" + name;
                //保存到数据库
               
                PhotoManager PM = new PhotoManager();
                decimal date =  decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                PHOTO photo = new PHOTO();
                photo.PHOTOPATH = fileName;
                photo.PHOTODATE = date;
                photo.LAST_UPDATER = MyStateManager.Name;
                photo.LAST_UPDATE_TIME = date;
                photo.DOMAINID = MyStateManager.DomainID;
                photo.USERID = MyStateManager.UserID;
                photo.PHOTOTYPEID = int.Parse(photoTypeID);
                if(PM.SavePhoto(photo)!= null)
                    return true;
            }

        }
        return false;
    }

   


    protected void btnGoto_Click(object sender, EventArgs e)
    {
        int page = 0;
        try
        {
            page = int.Parse(txtPage.Text.Trim());
        }
        catch { page = 0; }
        if (page > 0 & page <= pages)
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;

            for (int i = 1; i < page; i++)
            {
                start_record = end_record + 1;
                end_record = end_record + pagesize;
                curpage++;
            }

            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
            BindFrom(start_record, end_record);

        }

    }

    public void BindFrom(decimal start, decimal end)
    {
        string where = "";
        if (ViewState["where"] != null)
            where = ViewState["where"].ToString();

        dlPhotos.DataSource = PhotoManager.GetPhotoByPage(MyStateManager.DomainID, start, end, where);
        dlPhotos.DataBind();
    }

    protected void IMAGE_DELETE(object sender, EventArgs e)
    {
        LinkButton btnDelete = (LinkButton)sender;
        PhotoManager manager = new PhotoManager();
        manager.DeletePhoto(btnDelete.CommandArgument);
        BindFrom(1, pagesize);

    }

    protected void dlPhotos_ItemDataBound(object sender, DataListItemEventArgs e)
    {
       
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        String where = null;
        if (drpPhotoType.SelectedIndex != 0)
        {
            where = " AND PHOTO_TYPE_ID=" + drpPhotoType.SelectedValue;
        }
        count = PhotoManager.Count(MyStateManager.DomainID, where);
        ViewState["where"] = where;
        if (count > 0)
        {

            pages = Math.Ceiling(count / pagesize);//总页数
            start_record = 1;
            end_record = pagesize;
            curpage = 1;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
           
        }
        else
        {
            start_record = 0;
            end_record = 0;
            pages = 0;
            curpage = 0;
            txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", 0, 0, 0);
        }
        BindFrom(start_record, end_record);

    }

    protected String GetPhotoType(Object obj)
    {
        if (obj == null || DBNull.Value == obj)
            return null;
        return new DictionaryItemsManager().Load((decimal)obj).Name;
    }


    protected String GetDateTime(Object obj)
    {
        return BusinessLogic.GetDateTime(obj.ToString());
    }




    protected void btnPage_Click(object sender, EventArgs e)
    {
        Button btnPage = (Button)sender;
        if (btnPage.CommandName.Equals("FIRST"))
        {
            start_record = 1;
            end_record = pagesize;
            curpage = 1;
        }
        else if (btnPage.CommandName.Equals("PREVIOUS"))
        {
            if (curpage > 1)
            {
                end_record = start_record - 1;
                start_record = start_record - pagesize;
                curpage--;
            }
        }
        else if (btnPage.CommandName.Equals("NEXT"))
        {
            if (curpage < pages)
            {
                start_record = end_record + 1;
                end_record = end_record + pagesize;
                curpage++;
            }
        }
        else if (btnPage.CommandName.Equals("LAST"))
        {
            start_record = pagesize * (pages - 1) + 1;
            if (start_record <= 0)
                start_record = 1;
            end_record = count;
            curpage++;
        }
        txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pages, curpage);
        BindFrom(start_record, end_record);
    }

 


    protected void btnUpload_Click(object sender, EventArgs e)
    {
        FileUpload[] files = new FileUpload[] { FileUpload1, FileUpload2, FileUpload3, FileUpload4, FileUpload5 };
        HtmlSelect[] lists = new HtmlSelect[] { drpPhotoType1, drpPhotoType2, drpPhotoType3, drpPhotoType4, drpPhotoType5 };
        int count = 0;
        int i = 0;
        foreach (FileUpload file in files)
        {
            if (UploadImage(file, lists[i++].Value))
            {
                count++;
            }
        }
        if (count>0)
        {
            BindFrom(1, pagesize);
        }
    }
 
}